package travelDream.beans;

import java.util.List;

import javax.ejb.EJB;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;

import traveldream.ejb.interfaces.NoDeleteException;
import traveldream.ejb.interfaces.NotFindException;
import traveldream.ejb.interfaces.TransportationBeanLocal;
import traveldream.ejb.DTO.TransportationDTO;


@ManagedBean(name="transport")
@ViewScoped
public class TransportationManaged {
	
	private List<TransportationDTO> transportation;
	private TransportationDTO viewedT;
	
	public TransportationDTO getViewedT() {
		return viewedT;
	}

	public void setViewedT(TransportationDTO viewedT) {
		this.viewedT = viewedT;
		this.viewedT.setArrivalTime(viewedT.getArrivalTime());
	}

	@EJB
	private TransportationBeanLocal transportationBeanLocal;
	
	public TransportationManaged(){}
	
	public void getAllTransportation()
	{
		transportation=transportationBeanLocal.getAllTransport();
	}

	public String changePage(int id, char page){
		switch(page){
		case 'v':return "transportationView.xhtml?faces-redirect=true&id="+id;
		case 'e':return "transportationEdit.xhtml?faces-redirect=true&id="+id;
		default:return "error";
		}
	}
	
	public void handleError(){
		FacesContext facesContext = FacesContext.getCurrentInstance();
		String passedParameter = (facesContext.getExternalContext().getRequestParameterMap().get("error"));
		if(passedParameter==null) return;
		if(passedParameter.compareTo("nullPointer")==0)
		{
			FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR,"Error", "An error occured, please try again!!"));
		}
	}
	
	public void searchById() {
		FacesContext facesContext = FacesContext.getCurrentInstance();
		int passedParameter = Integer.parseInt((facesContext.getExternalContext().getRequestParameterMap().get("id")));
		try{
			setViewedT(transportationBeanLocal.find(passedParameter));
			System.out.println(viewedT.getArrivalTime().toString());
		}
		catch(NotFindException e){
			FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR,"Error", "Wrong id!! Please, go back and try again!!"));
		}
	}
	
	public void searchById(int id){
		try{
		setViewedT(transportationBeanLocal.find(id));
		}
		catch(NotFindException e){
			FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR,"Error", "Wrong id!! Please, go back and try again!!"));
		}
	}
	
	public String delete(int id)
	{
		try{
		transportationBeanLocal.delete(id);
		}
		catch(NotFindException e){
			return "transportationMean.xhtml?faces-redirect=true&error=nullPointer"; 
		}
		catch(NoDeleteException e){
			FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR,"Error", "You cannot delete!! Please delete fisrt the package in which it is"));
			return "";
		}
		return "transportationMean.xhtml?faces-redirect=true";
	}
	
	public List<TransportationDTO> getTransportation() {
		return transportation;
	}

	public void setTransportation(List<TransportationDTO> transportation) {
		this.transportation = transportation;
	}

	public TransportationBeanLocal getTransportationBeanLocal() {
		return transportationBeanLocal;
	}

	public void setTransportationBeanLocal(
			TransportationBeanLocal transportationBeanLocal) {
		this.transportationBeanLocal = transportationBeanLocal;
	}
	

}
